package com.aier.admin.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;



import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.admin.entity.ZhuLiao;

public class ZhuLiaoRowMapper implements RowMapper<ZhuLiao>{

    private int type;
    public static final int CAIPU_AND_FLG = 1;
    
    public ZhuLiaoRowMapper() {
    }
    
    public ZhuLiaoRowMapper(int type) {
        this.type = type;
    }

    @Override
    public ZhuLiao mapRow(ResultSet rs, int rowNum) throws SQLException {
        ZhuLiao cx = new ZhuLiao();
        
        switch (type) {
            case CAIPU_AND_FLG:
                cx = parseZhuLiao(new ZhuLiao(), rs);
                cx.setFlg_name(rs.getString("flg_name"));
                break;
            default:
                cx = parseZhuLiao(new ZhuLiao(), rs);
        }
        return cx;
    }

    public static ZhuLiao parseZhuLiao(ZhuLiao cx, ResultSet rs)
        throws SQLException {
        return parseZhuLiao(cx, rs, null);
    }

    public static ZhuLiao parseZhuLiao(ZhuLiao cx, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }

        try {
            cx.setZl_id(rs.getLong(su + "zl_id"));
        } catch (SQLException e) {
            cx.setZl_id(null);
        }
        try {
            cx.setZl_name(rs.getString(su + "zl_name"));
        } catch (SQLException e) {
            cx.setZl_name(null);
        }
        try {
            cx.setZl_img(rs.getString(su + "zl_img"));
        } catch (SQLException e) {
            cx.setZl_img(null);
        }      
        return cx;
    }

    public static Map<String, Object> getPar(Map<String, Object> par,
            ZhuLiao cx) {
        par.put("zl_id", cx.getZl_id());
        par.put("zl_name", cx.getZl_name());
        par.put("zl_img", cx.getZl_img());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args,
            ZhuLiao cx) {
        args.put("zl_id", cx.getZl_id());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final ZhuLiao cx) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(cx),
                        new String[] { "zl_id" });
                parsePS(ps, cx);
                return ps;
            }

        };
    }

    private static String parseSQL(ZhuLiao cx) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into userapp_zhuliao(");
        int count = 0;
        if (null != cx.getZl_id()) {
            sql.append("zl_id,");
            count++;
        }
        if (null != cx.getZl_name()) {
            sql.append("zl_name,");
            count++;
        }
        if (null != cx.getZl_img()) {
            sql.append("zl_img,");
            count++;
        }        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, ZhuLiao cx)
        throws SQLException {
        int count = 1;
        if (null != cx.getZl_id()) {
            ps.setLong(count++, cx.getZl_id());
        }
        if (null != cx.getZl_name()) {
            ps.setString(count++, cx.getZl_name());
        } 
        if (null != cx.getZl_img()) {
            ps.setString(count++, cx.getZl_img());
        } 
    }

}
